package com.hontech.bv.conn

import com.hontech.bv.codec.toHex
import com.hontech.bv.conn.serial.SerialType
import com.hontech.bv.utils.log

interface SerialPort {

    fun close()

    fun notifyClose()

    fun sync(): Int

    fun readBytes(buf: ByteArray): Int

    fun write(buf: ByteArray)

    private fun writePrintLog(buf: ByteArray) {
       // log("write:${buf.toHex()}")
        write(buf)
    }

    fun writeFrame(frame: WriteFrame) {
        val buf = Proto.makeRequest(frame.req, frame.dest, frame.seq, frame.args)
        synchronized(this) {
            writePrintLog(buf)
        }
    }

    fun writeAck(dest: Int, seq: Int) {
        val buf = Proto.makeAck(dest, seq)
        synchronized(this) {
            writePrintLog(buf)
        }
    }
}
